/**
 * mapping映射工具
 */
;(function (winObj) {

    const WIN = winObj ? winObj : window;

    const VERSION = "v1.0";

    let config = {};

    var _innerFunc = {
        /**
         * 功能: 判断值是否有效
         */
        isEmpty: function (value) {
            if (typeof value === "undefined" || value === null) {
                return true;
            }

            if (typeof value === "string") {
                return value === "";
            }

            if (value instanceof Array) {
                return value.length === 0;
            }

            return false;
        },

        /**
         * 功能: 判断值是否无效
         */
        isNotEmpty: function (value) {
            return !this.isEmpty(value);
        }
    };

    const MAPPING_UTIL = {
        /**
         * 功能: 获取版本号
         * @returns {string}
         */
        version: function () {
            return VERSION;
        },
        /**
         * 功能: 将映射关系mappings的值根据所配置的映射从fillDate中填入
         * @param mappings  映射关系, 数组类型
         * @param fillDate  填充数据, 格式{key: value}
         * @return {[]}
         */
        mappingHandle: function (mappings, fillDate) {
            if (_innerFunc.isEmpty(fillDate) || _innerFunc.isEmpty(mappings)) {
                throw new Error("传入的数据异常, 无法转换");
            }

            var result = [];

            for (var i = 0; i < mappings.length; i++) {
                var mapping = mappings[i];
                var tempNode = {};

                for (var insuKey in mapping) {
                    var mappingNode = mapping[insuKey];
                    var mappingValue = "";

                    if (typeof mappingNode === "string") {
                        if (mappingNode === "") {
                            mappingNode = insuKey;
                        }
                        // 如果配置的是string, 就直接获取
                        mappingValue = fillDate[mappingNode];
                    } else if (typeof mappingNode === "object") {
                        var mappingKey = null;
                        var mappingFunc = null;
                        var require = false;

                        if (mappingNode === null) {
                            // 如果未配置mapping, 则根据key取
                            mappingKey = insuKey;
                        } else {
                            // 获取转换属性
                            mappingKey = mappingNode["mappingKey"];
                            mappingFunc = mappingNode["mappingFunc"];
                            require = mappingNode["require"];
                        }

                        // 获取所配置的mapping对应的his中的值
                        mappingValue = fillDate[mappingKey];

                        var mappingValueBackup = mappingValue;
                        // 如果配置的转换函数不为空, 就进行调用
                        if (typeof mappingFunc === "function") {
                            try {
                                mappingValue = mappingFunc.call(null, mappingValue, fillDate);
                            } catch (e) {
                                mappingValue = mappingValueBackup;
                            }
                        }

                        // 判断结果是否为必须参数
                        if (require && _innerFunc.isEmpty(mappingValue)) {
                            throw new Error("[" + mappingKey + "]的值不能为空!");
                        }
                    }

                    if (_innerFunc.isEmpty(mappingValue)) {
                        mappingValue = "";
                    }

                    tempNode[insuKey] = mappingValue;
                }

                result.push(tempNode);
            }

            return result;
        }
    };

    WIN.MAPPING_UTIL = MAPPING_UTIL;

}.call(this));